<?php



function fill_set_from_table($table_id, $project_id, $crudset_id, $thetype){

// Get information for variables with chosen tablesource
	$qry_table_vars = "SELECT * FROM variables WHERE source_id = $table_id AND project_id = $project_id";
		
	$table_vars = get_array_query($qry_table_vars);
	
	$table_name = lookup_value('table_name', 'tablesources', "tablesource_id = $table_id");
	$next_crudset_id = get_max_value ('crudset_id', 'crudsets', 1) + 1; 
	
	//$cs_name = lookup_value('cs_name', 'crudsets', "crudset_id = $crudset_id");
	$subdirname = $table_name . '/';
	
	$qry_new_crudset = "INSERT INTO crudsets(crudset_id, cs_name, project_id, main_table, cs_type, subdirectory)
	VALUES ($next_crudset_id, '$table_name', $project_id, $table_id, '$thetype', '$subdirname' )";
	$result = mysql_query($qry_new_crudset) or print (mysql_error());
	//print "<br> $qry_new_crudset <br/>\n";
	
	if($thetype = 'readonly'){
		$editable = 0;
		$filter = 0;
	
	}
	else{
		$editable = 1;
		$filter = 1;	
	}
	
	foreach($table_vars as $the_var){
		$variable_id = $the_var['variable_id'];
		$is_key = lookup_value('is_key', 'variables', "variable_id = $variable_id");
		$qry_new_col = "INSERT INTO crudcols (crudset_id, variable_id, is_key, shown_in_list, edit_in_list, filter) 
		VALUES ($next_crudset_id, $variable_id, $is_key, 1, '1','1')";

		$result = mysql_query($qry_new_col) or print (mysql_error() . $qry_new_col);		
		
	}
	
}


function get_table_vars($table, $project_id, $alias = '', $table_filter = '1'){
	if($alias == ''){$alias = $table;}
	//print "In table vars";

	$dbServer = lookup_value('project_server', 'projects', " project_id = $project_id ");
	$dbUsername = lookup_value('project_login', 'projects', " project_id = $project_id ");
	$dbDatabase = lookup_value('project_db', 'projects', " project_id = $project_id ");
	$dbPassword = lookup_value('project_pass', 'projects', " project_id = $project_id ");

	//print "got $dbServer , $dbUsername, $dbDatabase, $dbPassword ";
	
	$colinfo = get_mysql_table_info($dbServer,$dbUsername,$dbDatabase,$dbPassword,$table);

	$qry_table_insert = "INSERT INTO tablesources (project_id, table_name, table_alias, table_filter) 
	VALUES ($project_id, '$table', '$alias', '$table_filter')";
	

	include "quickcrud_connect.php";
	//print $qry_table_insert . "<br>\n";
	$result = mysql_query($qry_table_insert) or print (mysql_error());

	$table_id = lookup_value('tablesource_id', 'tablesources', " project_id = $project_id AND table_alias = '$alias' ");
		

	foreach ($colinfo AS $varcol){
		$qry_insert_var = "INSERT INTO variables (variable_name, type, project_id, source_id, is_key) 
		VALUES('" . $varcol['variable_name'] . "', '" . $varcol['type'] . "', $project_id, $table_id, " . $varcol['is_key'] . " )";
		//print $qry_insert_var . "<br>\n";
		
		//include "quickcrud_connect.php";
		
		$result = mysql_query($qry_insert_var) or print (mysql_error());
		}
	
	
}

function get_mysql_table_info($dbServer,$dbUsername,$dbDatabase,$dbPassword,$table){
	$workdb = mysql_connect($dbServer, $dbUsername, $dbPassword) or die(mysql_error());
	//print "Got to here 3";
	mysql_select_db($dbDatabase,$workdb)  or die(mysql_error());
	
	$qry_get_columns = "SHOW COLUMNS IN $table";
	
	$get_results = mysql_query($qry_get_columns, $workdb) or die(mysql_error());
	//print "GotShowVars";
	if(mysql_num_rows($get_results)) {
		$index = 1;
		while($resultsarray = mysql_fetch_assoc($get_results)) {
			$valuesarray[$index] = $resultsarray;
			
			$colinfo[$index]['variable_name'] = $resultsarray['Field'];
			$colinfo[$index]['type'] = $resultsarray['Type'];
			$colinfo[$index]['size'] = 20;
			if($resultsarray['Key'] == 'PRI'){
				$colinfo[$index]['is_key'] = 1;
			}
			else{
				$colinfo[$index]['is_key'] = 0;
			}
			$index ++;
			}
		
		}
		mysql_close($workdb);
	return $colinfo;

}



?>